home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / drgnsmth.cpt / Dragonsmith 1.1 / Base files / Utilities / HandleUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-10  |  2.3 KB  |  101 lines

  1. /*
  2.     HandleUtils.c
  3.     
  4.     Created    28 Mar 1992    NeedHandle only
  5.     
  6.     Modified    14 Apr 1992    Added more functions
  7.             19 Apr 1992    NeedHandle will now resize the handle ╤ duh!
  8.             28 May 1992    Fixed idiocy in HandleToScrap (it was ignoring the type parameter)
  9.             29 Aug 1992    Added a return result to ResizeHandle (what was I thinking?)
  10.                         Added CopyHandle
  11.             11 Sep 1992    Removed GrowByAndPoint, NeedHandle, and BigAnyHandle
  12.  
  13.     Copyright ⌐ 1992 by Paul M. Hoffman
  14.     Send comments or suggestions to paul.hoffman@um.cc.umich.edu
  15.     
  16.     This source code may be freely used, altered, and distributed in any way as long as:
  17.         1.    It is GIVEN away rather than sold (except as expressly permitted by the author)
  18.         2.    This statement and the above copyright notice are left intact.
  19.  
  20. */
  21.  
  22. #include    "HandleUtils.h"
  23.  
  24. OSErr CopyHandle (Handle srcHndl, Handle destHndl)
  25. {
  26.     long        srcSize;
  27.     OSErr    err;
  28.     
  29.     if (srcHndl == NULL || destHndl == NULL || *srcHndl == NULL || *destHndl == NULL)
  30.         return nilHandleErr;
  31.     
  32.     srcSize = GetHandleSize (srcHndl);
  33.     err = ResizeHandle (destHndl, srcSize);
  34.     if (err != noErr)
  35.         return err;
  36.         
  37.     BlockMove (*srcHndl, *destHndl, srcSize);
  38.     return noErr;
  39. }
  40.  
  41. OSErr ResizeHandle (Handle h, long newSize)
  42. {
  43.     char        hState;
  44.     OSErr    err;
  45.     
  46.     hState = HGetState (h);
  47.     HUnlock (h);
  48.     SetHandleSize (h, newSize);
  49.     err = MemError ();
  50.     HSetState (h, hState);
  51.     
  52.     return err;
  53. }
  54.  
  55. Handle BigHandle (long *actualSize)
  56. {
  57.     // Get a handle to as big a block as you can without getting too fancy
  58.     Handle    h = NULL;
  59.     long        sz;
  60.     
  61.     // We AND sz to make sure it's a positive value; otherwise the >> may wreak havoc
  62.     //    ╤ nobody should be asking for a 2 Gigabyte block, but you never know╔
  63.     for (sz = *actualSize & 0x7FFFFFFF; h == NULL && sz > 0; sz = (sz >> 1) + 1) {
  64.         h = NewHandle (sz);
  65.         if (h != NULL)
  66.             *actualSize = sz;
  67.     }
  68.     if (h == NULL) *actualSize = 0L;
  69.     return h;
  70. }
  71.  
  72. Handle AnyHandle (long size)
  73. {
  74.     Handle    h;
  75.     OSErr    err;
  76.     
  77.     return ((h = NewHandle (size)) ? h : TempNewHandle (size, &err));
  78. }
  79.  
  80. OSErr HandleToScrap (Handle h, ResType type)
  81. {
  82.     long        err;
  83.     char        hState;
  84.     
  85.     err = ZeroScrap ();
  86.     if (err == 0L) {
  87.         hState = SmartHLock (h);
  88.         err = PutScrap (GetHandleSize (h), type, *h);
  89.         HSetState (h, hState);
  90.         SystemEdit (4);        // Make the system think the user chose "Copy" from the Edit menu
  91.     }
  92.     return (short) err;
  93. }
  94.  
  95. char SmartHLock (Handle h)
  96. {
  97.     char        hState = HGetState (h);
  98.     
  99.     HLock (h);
  100.     return hState;
  101. }